package carol.pvp.analyzer.java.impl;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import wangy.pvp.basiclibrary.basicKey.ClassAnalysisKey;
import wangy.pvp.basiclibrary.basicType.AssArg;
import wangy.pvp.basiclibrary.basicType.AssBasicType;
import wangy.pvp.basiclibrary.basicType.AssClass;
import wangy.pvp.basiclibrary.basicType.AssMethod;
import carol.pvp.analyzer.impl.CodeInfoCatcher;
import carol.pvp.analyzer.java.pattern.JavaCatcherPattern;

/**
 * 
 * @author carol
 *
 */
public class JavaCodeInfoCatcher extends CodeInfoCatcher{
	AssBasicType info = null;
	
	protected AssBasicType catchPackge(String code){
		info = new AssClass();
		
		info.setType(ClassAnalysisKey.PACKAGE_TYPE);
		((AssClass) info).setPackageName(getPackageName(code));
        
		return info;
	}
	
	protected AssBasicType catchClass(String code){
		info = new AssClass();
		
		info.setType(ClassAnalysisKey.CLASS_TYPE);
		((AssClass) info).setIdentifier(getModifier(code));
		((AssClass) info).setClassName(getClassName(code));
		((AssClass) info).addSuperClasses(getClassSuper(code));
        
		return info;
	}
	
	protected AssBasicType catchMethod(String code){
		info = new AssMethod();
		
		info.setType(ClassAnalysisKey.METHOD_TYPE);
		((AssMethod) info).setIdentifier(getModifier(code));
		((AssMethod) info).setName(getMethodName(code));
		
		ArrayList<AssArg> paraList = getMethodPara(code);
		for(int i=0;i<paraList.size();i++){
			((AssMethod) info).addArg(paraList.get(i));
		}
		
		return info;
	}
	
	protected AssBasicType catchOther(){
		info = new AssBasicType();
		
		info.setType("");
		
		return info;
	}
	
	
	private String getPackageName(String code){
		Pattern pattern = Pattern.compile(JavaCatcherPattern.PACKAGE_NAME_PATTERN);
        Matcher matcher = pattern.matcher(code);   
        if(matcher.find()){
        	return matcher.group(1);
        }
        
        return "";
	}
	
	private String getModifier(String code){
		Pattern pattern = Pattern.compile(JavaCatcherPattern.MODIFIER_PATTERN);
        Matcher matcher = pattern.matcher(code);
        if(matcher.find()){
        	return matcher.group(0);
        }else{
        	return "default";
        }
	}
	
	private String getClassName(String code){
		Pattern pattern = Pattern.compile(JavaCatcherPattern.CLASS_NAME_PATTERN);
        Matcher matcher = pattern.matcher(code);
        if(matcher.find()){
        	return matcher.group(1);
        }
        
        return "";
	}
	
	private String getClassSuper(String code){
		Pattern pattern = Pattern.compile(JavaCatcherPattern.CLASS_SUPER_PATTERN);
        Matcher matcher = pattern.matcher(code);
        if(matcher.find()){
        	return matcher.group(1);
        }
        
        return "";
	}
	
	private String getMethodName(String code){
		Pattern pattern = Pattern.compile(JavaCatcherPattern.METHOD_NAME_PATTERN);
        Matcher matcher = pattern.matcher(code);
        if(matcher.find()){
        	return matcher.group(1);
        }
        
        return "";
	}
	
	private ArrayList<AssArg> getMethodPara(String code) {
		ArrayList<AssArg> paraList = new ArrayList<AssArg>();
		
		Pattern pattern = Pattern.compile(JavaCatcherPattern.METHOD_PARA_PATTERN);
        Matcher matcher = pattern.matcher(code);
        
        if(matcher.find()){
        	String paraStr = matcher.group(1);
        	String[] paras = paraStr.split(",");
        	for(int i=0;i<paras.length;i++){
        		String[] para = paras[i].trim().split(" ");
        		if(para.length>1){
        			AssArg arg = new AssArg();
            		arg.setType(para[0].trim());
            		arg.setName(para[para.length-1].trim());
            		paraList.add(arg);
        		}
        	}
        }
        
		return paraList;
	}
}
